#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
#include "Delay.h"
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array, uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number, uint8_t Length);
void Serial_Printf(char *format, ...);

char Serial_RxPacket[200];				//"+MSG\r\n"
uint8_t Serial_RxFlag = 0;

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &USART_InitStructure);
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
	//连接阿里云
	Serial_SendString("AT+RST\r\n");
	Delay_ms(1000);
	
	Serial_SendString("AT+CWMODE=3\r\n");
	Delay_ms(1000);
	

	Serial_SendString("AT+CWJAP=\"12345678\",\"87654321.\"\r\n");
	Delay_ms(3000);
	
	Serial_SendString("AT+CIPSNTPCFG=1,8,\"ntp1.aliyun.com\"\r\n");
	Delay_ms(1000);
	
	Serial_SendString("AT+MQTTCLEAN=0\r\n");
	Delay_ms(1700);
	Serial_SendString("AT+MQTTUSERCFG=0,1,\"NULL\",\"LOCK&i7k4aSDhZQr\",\"a6ee001c63f67f0ee1b7f7b80cdfe91c53ef291812954a98131803212e0a54a0\",0,0,\"\"\r\n");
	Delay_ms(1000);
	
	
	Serial_SendString("AT+MQTTCLIENTID=0,\"i7k4aSDhZQr.LOCK|securemode=2\\,signmethod=hmacsha256\\,timestamp=1681183549723|\"\r\n");
	Delay_ms(1000);
	
	Serial_SendString("AT+MQTTCONN=0,\"iot-06z00dpap5qm1m5.mqtt.iothub.aliyuncs.com\",1883,1\r\n");
	Delay_ms(1000);

//	
	//Serial_SendString("AT+MQTTSUB=0,\"/sys/i7k4aSDhZQr/LOCK/thing/event/property/post_reply\",1\r\n");

//	Delay_ms(1000);
//	
//	
//	Serial_SendString("AT+MQTTSUB=0,\"/i7k4aSDhZQr/LOCK/user/get\",1\r\n");

//	Delay_ms(1000);
//	Serial_SendString("AT+MQTTPUB=0,\"/i7k4aSDhZQr/LOCK/user/update\",\"LCOK\",1,0\r\n");
//	Delay_ms(1000);
//	Serial_SendString("AT+MQTTSUB=0,\"/i7k4aSDhZQr/${deviceName}/user/topic\",1\r\n");

//	Delay_ms(1000);
//	Serial_SendString("AT+MQTTPUB=0,\"/i7k4aSDhZQr/LOCK/user/update\",\"LCOK\",1,0\r\n");
//	Delay_ms(1000);

}

void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Array[i]);
	}
}

void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);
	}
}

uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y --)
	{
		Result *= X;
	}
	return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');
	}
}

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}


void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t pRxPacket = 0;
	
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		
		uint8_t RxData = USART_ReceiveData(USART1);
		
		if (RxState == 0)
		{
			if (RxData == '+' ) //&& Serial_RxFlag == 0
			{	
				RxState = 1;
				pRxPacket = 0;
			}
		}
		else if (RxState == 1)
		{
			if (RxData == '\r')
			{
				RxState = 2;
			}
			else
			{
				Serial_RxPacket[pRxPacket] = RxData;
				pRxPacket ++;
				
			}
		}
		else if (RxState == 2)
		{
			if (RxData == '\n')
			{
				RxState = 0;
				Serial_RxPacket[pRxPacket] = '\0';
				Serial_RxFlag = 1;
				
			}
		}
		
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}
